<template>
<vs-modal :show.sync="show" effect="fade" width="800px">
  <div class="modal-header" slot="modal-header">
    <h4 class="modal-title">{{$t('common.detail')}}</h4>
  </div>  
  <div class="modal-body" slot="modal-body">
    <form class="form-horizontal">
      <div class="form-group">
        <label class="col-sm-3 control-label">{{$t('list.name')}}</label>
        <div class="col-sm-8">
          <input type="text" class="form-control" v-model="cfg.name">
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-3 control-label">{{$t('list.multiScreen')}}</label>
        <div class="col-sm-8">
          <p class="form-control-static">{{cfg.multiScreen}}</p>
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-3 control-label">{{$t('list.layout')}}</label>
        <div class="col-sm-8" id="layout">
        </div>
      </div>      
    </form>
  </div>
  <div class="modal-footer" slot="modal-footer">
    <button type="button" class="btn btn-default" @click.prevent="onQuit">{{$t('common.quit')}}</button>
    <button type="button" class="btn btn-primary" @click.prevent="onSaveChanges">{{$t('common.savechanges')}}</button>
  </div>    
</vs-modal>  
</template>

<script>
import _ from 'lodash'
import ebus from 'libs/ebus.js'
import toast from 'libs/toast.js'
import locales from '../locales/vidLayout.js'
import {printObj, copyObj} from 'libs/utils.js'
import {LcdPreview} from 'libs/canvas.js'
import * as actions from 'share/server/vuex/actions/multimedia.js'
import {multimediaApi} from 'share/server/api.js'

export default {
  locales,
  vuex: {
    getters: {
      streams: (state) => state.streamsrc.list
    },
    actions
  },
  data () {
    return {
      show: false,
      cfg: {
        _id: 0,
        name: '',
        multiScreenX: 1,
        multiScreenY: 1,
        multiScreen: '',
        windowList: ''
      }
    }
  },
  methods: {
    onQuit () {
      this.show = false
    },
    onSaveChanges () {
      if (this.cfg.name === '') {
        toast.w(this, this.$t('common.fail'), this.$t('error.params'))
        return
      }

      multimediaApi.setItem('vidLayouts', {
        _id: this.cfg._id,
        name: this.cfg.name,
        multiScreenX: this.cfg.multiScreenX,
        multiScreenY: this.cfg.multiScreenY,
        windowList: this.cfg.windowList
      }).then(function(res) {
        if (res && res.data && res.data.code === 0) {
          this.doLoadVidLayouts()
        } else {
          toast.code(this, res.data)
        }
      }.bind(this))
      this.show = false
    },
    onDrawVidLayout () {
      let layout = this.cfg
      var windows = JSON.parse(layout.windowList)
      _.forEach(windows, function(item) {
        let stream = _.find(this.streams, {topoId: item.srcTopoId, portName: item.srcPortName})
        if (stream) {
          item.srcName = stream.name
        }
        var coordinate = item.coordinate
      }.bind(this))

      let realWidth = 12 * layout.multiScreenX * 16
      let realHeight = 12 * layout.multiScreenY * 9
      let scale = 500 / realWidth
      var previewCfg = {
        width: 500,
        height: realHeight * scale,
        scale: scale
      }
      this.preview.init('layout', previewCfg)     
      this.preview.updateBg({
        color: {
          hex: '#666'
        }
      }) 
      this.preview.updateWindows(windows, {
        xScale: scale * 16,
        yScale: scale * 9
      })
    },
    onShow (val) {
      copyObj(this.cfg, val)
      this.onDrawVidLayout()
      this.show = true
    }
  },
  ready () {   
    this.preview = new LcdPreview()
    ebus.on('show-vid-layout-detail', this.onShow)
  }
}
</script>

<style>
</style>